package org.mil.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TextUtil {

	public static final SimpleDateFormat sdf1 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat sdf2 = new SimpleDateFormat(
			"z yyyy-MM-dd HH:mm:ss");
	private static final NumberFormat cnf = NumberFormat.getCurrencyInstance();
	private static final DecimalFormat df2 = new DecimalFormat("#0.00");
	private static final DecimalFormat df4 = new DecimalFormat("#0.0000");

	public static String Capitalize1stLetter(String str) {

		char c = str.charAt(0);

		if (c > 96 && c < 123)
			return (char) (c - 32) + str.substring(1);
		return str;
	}

	/**
	 * 去量纲后缀，将一个物理量纯数学化 如quantify("120kg", "kg")=120
	 * 
	 * @param quantity
	 *            物理量表达式
	 * @param dimension
	 *            量纲后缀
	 * @return
	 */
	public static double quantify(String quantity, String suffix) {
		if (quantity == null)
			return -1;
		if (suffix == null || suffix == "")
			return quantify(quantity);
		if (quantity.endsWith(suffix))
			try {
				int x = quantity.indexOf(suffix);
				return Double.valueOf(condense(quantity.substring(0, x)));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		else
			return quantify(quantity);
		return 0;

	}

	public static double quantify(String quantity) {
		if (quantity == null)
			return -1;
		try {
			return Double.valueOf(condense(quantity));
		} catch (NumberFormatException e) {
			try {
				System.out.println("WARNING:reluctant coercion : '" + quantity
						+ "'");
				String str = quantity.trim().split(" ")[0];
				return Double.valueOf(str);
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			}
			return 0;
		}

	}

	public static int getMinutes(String h_min) {
		if (h_min == null)
			return -1;
		int min = 0;
		int hx = h_min.indexOf('h');
		if (hx >= 0)
			try {
				min += Integer.valueOf(condense(h_min.substring(0, hx))) * 60;
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}

		int mx = h_min.indexOf("mn");
		if (mx < 0)
			mx = h_min.indexOf("min");
		if (mx < 0)
			return -1;
		try {
			min += Integer.valueOf(condense(h_min.substring(hx + 1, mx)));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return min;

	}
	/**
	 * 时间-字符串转换
	 * @param str
	 * @return
	 */
	public static Calendar parseDate(String str) {
		if (str == null)
			return null;
		Calendar c = Calendar.getInstance();

		Date date;
		try {
			date = sdf2.parse(str);

		} catch (ParseException e) {
			e.printStackTrace();
			return c;

		}

		c.setTime(date);
		return c;
	}

	public static String formatTime(Calendar time) {
		if (time == null)
			return null;
		return sdf1.format(time.getTime());
		
	}
	
	public static String formatDate(Calendar time) {
		if (time == null)
			return null;
		return sdf2.format(time.getTime());
	}
	
	/**
	 * 去掉所有空格
	 * 如"1 250 000 dollars"变成"1250000dollars"
	 * @param arg
	 * @return
	 */
	public static String condense(String arg) {
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < arg.length(); i++)
			if (arg.charAt(i) != ' ')
				sb.append(arg.charAt(i));
		return sb.toString();

	}

	public static String getCurrencyStyle(double number) {
		return cnf.format(number);
	}

	public static String get2FracStyle(double number) {
		return df2.format(number);
	}

	public static String get4FracStyle(double number) {
		return df4.format(number);
	}

	public static void main(String[] arg) {
		// System.out.println((int) 'A');
		// System.out.println(Capitalize1stLetter("Zbc"));
		System.out.println(getMinutes(" 3h24min gfg"));
		// System.out.println(quantify("120 kg", "ll"));
		// System.out.println(parseDate("UTC 2010-03-23 10:10:52").getTime());
		// System.out.println(sdf2.format(new Date()));
		System.out.println(condense("  ke  rk ke k ii  "));

	}
}
